package com.tanhua.app.controller;

import com.tanhua.app.Interceptor.UserHolder;
import com.tanhua.app.exception.SettingsService;
import com.tanhua.app.service.BlackListService;
import com.tanhua.app.service.QuestionService;
import com.tanhua.app.service.UserInfoService;
import com.tanhua.model.pojo.UserInfo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import com.tanhua.model.vo.UserInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/users")
public class UsersController {

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private SettingsService settingsService;

    @Autowired
    private QuestionService questionService;

    @Autowired
    private BlackListService blackListService;


    /**
     * 根据用户id查询用户信息
     * @return
     */
    @GetMapping
    public ResponseEntity findById(Long userID, @RequestHeader("Authorization") String token) {

/*        // 1. token鉴权
        boolean verifyToken = JwtUtils.verifyToken(token);

        // 2. 校验
        if (!verifyToken) {
            return ResponseEntity.status(HttpStatus.HTTP_UNAUTHORIZED).body(null);
        }

        // 3. 获取token中的id值
        Long id = Long.parseLong(JwtUtils.getClaims(token).get("id").toString());*/

        // 从ThreadLocal中获取数据
        Long id = UserHolder.getUserId();

        // 3.1 判断userId是否为空，为空将登陆用户id传入
        if (userID == null) {
            userID = id;
        }

        // 4. 数据调用
        UserInfoVo vo = userInfoService.findById(userID);

        // 5. 返回数据
        return ResponseEntity.ok(vo);

    }


    /**
     * 用户资料 - 更新
     * PUT/users
     */
    @PutMapping
    public ResponseEntity updateById(@RequestBody UserInfo userInfo) {
/*                                     @RequestHeader("Authorization") String token) {

        // 1. token鉴权
        boolean verifyToken = JwtUtils.verifyToken(token);

        // 2. 校验
        if (!verifyToken) {
            return ResponseEntity.status(HttpStatus.HTTP_UNAUTHORIZED).body(null);
        }

        // 3. 获取token中的id值
        Long id = Long.parseLong(JwtUtils.getClaims(token).get("id").toString());*/

        // 从ThreadLocal中获取数据
        Long id = UserHolder.getUserId();

        userInfo.setId(id);

        // 4. 调用方法，更新数据
        userInfoService.updateById(userInfo);

        // 5. 返回数据
        return ResponseEntity.ok(null);
    }


    /**
     * 用户通用设置 - 读取
     * GET/users/settings
     */
    @GetMapping("/settings")
    public ResponseEntity settings() {
        // 调用方法，获取数据
        SettingsVo vo = settingsService.settings();

        // 返回数据
        return ResponseEntity.ok(vo);
    }

    /**
     * 设置陌生人问题 - 保存
     * POST/users/questions
     */
    @PostMapping("/questions")
    public ResponseEntity questions(@RequestBody Map<String,String> params) {
        // 获取问题数据
        String content = params.get("content");

        // 执行业务层方法
        questionService.questions(content);

        // 返回结果
        return ResponseEntity.ok(null);
    }


    /**
     * 通知设置 - 保存
     * POST/users/notifications/setting
     */
    @PostMapping("/notifications/setting")
    public ResponseEntity notifications(@RequestBody Map<String,Boolean> params) {

        // 执行方法
        settingsService.saveSetting(params);

        return ResponseEntity.ok(null);
    }


    /**
     * 黑名单 - 翻页列表
     * GET/users/blacklist
     */
    @GetMapping("/blacklist")
    public ResponseEntity<PageResult<UserInfo>> blacklist(@RequestParam(defaultValue = "1", required = false) Integer page,
                                                          @RequestParam(defaultValue = "10", required = false) Integer pagesize) {

        // 执行方法
        PageResult<UserInfo> pageResult = blackListService.findBlackPage(page, pagesize);

        return ResponseEntity.ok(pageResult);
    }


    /**
     * 黑名单 - 移除
     * DELETE/users/blacklist/{uid}
     */
    @DeleteMapping("/blacklist/{uid}")
    public ResponseEntity deleteBlacklist(@PathVariable Long uid) {

        // 执行方法
        blackListService.update(uid);

        return ResponseEntity.ok(null);

    }

}
